/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.comment;

import cn.hutool.core.util.StrUtil;
import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.CommentOrderSuccessEvent;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.OmsOrder;
import com.lxk.oms.order.domain.bo.OmsOrderCommentBo;
import com.lxk.oms.order.domain.bo.OmsOrderCommentItemBo;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderCommentItemService;
import com.lxk.oms.order.service.IOmsOrderCommentService;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;

import java.util.Date;
import java.util.List;

import static com.lxk.oms.flow.comment.FlowCommentOrderNodeConst.COMMENT_ORDER_NODE;

/**
 * 评价订单
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(COMMENT_ORDER_NODE)
public class OrderCommentNode extends NodeComponent {
    private final IOmsOrderService omsOrderService;
    private final IOmsOrderCommentService commentService;
    private final IOmsOrderCommentItemService omsOrderCommentItemService;

    @Override
    public void process() throws Exception {
        CommentOrderContext contextBean = this.getContextBean(CommentOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        Long orderId = omsOrderVo.getId();
        OmsOrder update = new OmsOrder();
        update.setFinishTime(new Date());
        Boolean success = omsOrderService.updateOrderStatusWithInfo(orderId, OrderEnums.OrderStatus.WAIT_EVALUATION_370,
            OrderEnums.OrderStatus.CONFIRM_400, update);
        if (success) {
            // 评价
            OmsOrderCommentBo add = MapstructUtils.convert(contextBean, OmsOrderCommentBo.class);
            add.setOrderId(omsOrderVo.getId());
            Boolean b = commentService.insertByBo(add);
            if (b) {
                List<OmsOrderCommentItemBo> itemList = contextBean.getItemList();
                itemList.stream().forEach(item -> {
                    item.setCommentId(add.getId());
                    omsOrderCommentItemService.insertByBo(item);
                });
            }
        } else {
            contextBean.setMsg(StrUtil.format("当前订单状态:{}, 无法评论", OrderEnums.OrderStatus.getByCode(omsOrderVo.getStatus()).getName()));
        }
        contextBean.setSuccess(success);
    }

    @Override
    public void onSuccess() throws Exception {
        CommentOrderContext contextBean = this.getContextBean(CommentOrderContext.class);
        if (contextBean.getSuccess()) {
            // 发送操作事件
            OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
            OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
            if (contextBean.getSendOptionEvent()) {
                optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
                optionLogEvent.setUserId(contextBean.getUserId());
                optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
                optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
                optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.COMMENT_ORDER.formatContent(contextBean.getComment()));
                optionLogEvent.setUserType(contextBean.getUserType());
                optionLogEvent.setMerId(omsOrderVo.getMerId());
                optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
                optionLogEvent.setTenantId(contextBean.getTenantId());
                EventHelper.publishEvent(optionLogEvent);
            }
            // 发送成功事件
            CommentOrderSuccessEvent event = new CommentOrderSuccessEvent();
            event.setOrderNo(omsOrderVo.getOrderNo());
            event.setUserId(contextBean.getUserId());
            EventHelper.publishEvent(event);
        }
    }
}
